import Base from 'models/base';

import moment from 'moment';

class AuctionRecord {
  constructor(record) {
    this.id = record.activityLogId;
    this.userName = record.nickname;
    this.unionid = record.playerUnionid;
    this.time = moment(record.createTime,'MM-DD HH:mm:ss').valueOf();
    this.price = record.activityPrice;
  }
}

export default class GoodsAuctionDetail extends Base {
  constructor(auction, systemTime) {
    super();

    this.activityTypeId = auction.activityTypeId;
    this.defId = auction.goodsDefId;
    this.goodId = auction.activityGoodsId;
    this.modelId = auction.goodsModelId;
    this.branchId = auction.branchId;
    this.partnerId = auction.partnerId;
    this.name = auction.goodsName;
    this.pic = this.getOnePic(auction.goodsPic);
    this.price = auction.activityPrice;
    this.description = auction.goodsDescription;
    this.tip = auction.goodsTips;
    this.sourcePrice = auction.goodsModelSourcePrice;

    let startTime = auction.activityStartTime;
    this.startTime = moment(startTime).valueOf();

    let endTime = auction.activityEndTime;
    this.endTime = moment(endTime).valueOf();

    this.maxRecordId = null;
    this.maxRecord = null;
    this.auctionRecordList = [];
    this.buyPrice = 0;
    this.buyPriceRes = 0;

    this.isJoin = auction.payOneYuan === 1;
    this.isPay = auction.payOneYuanLast === 1;

    this.setAuctionType(systemTime);
  }

  setAuctionType(nowTime) {
    if (nowTime === undefined) {
      nowTime = moment();
    } else {
      nowTime = moment(nowTime)
    }

    let startTime = this.startTime;
    let endTime = this.endTime;
    let type;
    if (moment(nowTime).isBefore(startTime, 'second')) {
      type = 0;
    } else if (moment(nowTime).isAfter(endTime, 'second')) {
      type = 2;
    } else {
      type = 1;
    }
    this.type = type;
  }

  setAuctionRecordList(recordList, isInit) {
    let records = [];
    let maxRecordId = this.maxRecordId;
    for (let i = 0; i < recordList.length; i++) {
      let record = recordList[i];
      let id = record.activityLogId;
      if (id <= maxRecordId) {
        continue;
      }

      record = new AuctionRecord(record);
      records.push(record);
    }

    let auctionRecordList = this.auctionRecordList;
    if (records.length !== 0) {
      let maxRecord = records[0];
      this.maxRecord = maxRecord;
      this.maxRecordId = maxRecord.id;
      this.price = maxRecord.price;
      auctionRecordList.unshift(...records);
    } else {
      if (!isInit) {
        return;
      }
    }

    if (this.type === 2) {
      this.buyPrice = this.price;
      this.buyPriceRes = this.price;
      return;
    }

    if (auctionRecordList.length === 0) {
      this.buyPrice = this.price;
      this.buyPriceRes = this.price;
    } else {
      let len = auctionRecordList.length;
      if (len > 200) {
        auctionRecordList.splice(200, len - 200);
      }

      let maxRecord = this.maxRecord;
      let price = maxRecord.price + 1;
      if (this.buyPriceRes < price) {
        this.buyPriceRes = price;
      }
      if (this.buyPrice < price) {
        this.buyPrice = price;
      }
    }
  }

  subtractPrice(value = 1) {
    let buyPrice = this.buyPrice;
    buyPrice -= value;
    if (buyPrice >= this.buyPriceRes) {
      this.buyPrice = buyPrice;
    }
  }

  addPrice(value = 1) {
    let buyPrice = this.buyPrice;
    buyPrice += value;
    if (buyPrice <= this.buyPriceRes + 10) {
      this.buyPrice = buyPrice;
    }
  }

  getPayJoinPriceObject() {
    let postData = {
      activityTypeId: this.activityTypeId,
      goodsDefId: this.defId,
      goodsModelId: this.modelId,
      shouldPayPrice: 1,
      branchId: this.branchId
    };

    let partnerId = this.partnerId;
    if (partnerId !== null) {
      postData.partnerId = partnerId;
    }

    return postData;
  }

  getPayObject(){
    let postData = {
      goodsDefId: this.defId,
      goodsModelId: this.modelId,
      buyCount: 1,
      activityTypeId: 3
    };

    return postData;
  }
}
